/*
 * Chud.cpp
 *
 *      Author: Boboss
 */

#include "Chud.h"

Chud::Chud(int width, int height)
{
	_numberOfBomb = "Number of bombs: ";
	_numberOfBombDiffuse = "Number of bombs diffused : ";


	_width = width;
	_height = height;
	_nbBomb = 0;
	_nbBombDiffuse = 0;
	_bombTimer = 0;
	_bombTimerMax = 0;
	_timeLeft = 0;

	_nbOfBomb = new osgText::Text;
	_nbOfBombDiffuse = new osgText::Text;
	_timeLeft = new osgText::Text;
	_frameRate = new osgText::Text;

	_hudGeode = new osg::Geode;
	_hudProjection = new osg::Projection;
	_hudProjection->setMatrix(osg::Matrix::ortho2D(0,_width,0,_height));
	_hudModelViewMatrix = new osg::MatrixTransform;
	_hudModelViewMatrix->setMatrix(osg::Matrix::identity());
	_hudModelViewMatrix->setReferenceFrame(osg::Transform::ABSOLUTE_RF);

	_hudProjection->addChild(_hudModelViewMatrix.get());
	_hudModelViewMatrix->addChild( _hudGeode.get() );

	_hudStateSet = new osg::StateSet();
	_hudGeode->setStateSet(_hudStateSet.get());
	_hudStateSet->setTextureAttributeAndModes(0,0,osg::StateAttribute::ON);
	_hudStateSet->setMode(GL_BLEND,osg::StateAttribute::OFF);
	_hudStateSet->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
	_hudStateSet->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
	_hudStateSet->setRenderBinDetails( 11, "RenderBin");

	_hudGeode->addDrawable( _nbOfBomb.get());
	_hudGeode->addDrawable( _nbOfBombDiffuse.get());
	_hudGeode->addDrawable( _timeLeft.get());
	_hudGeode->addDrawable( _frameRate.get());

	_nbOfBomb->setCharacterSize(20);
	_nbOfBomb->setFont("./ressource/AllStarResort.ttf");

	_nbOfBombDiffuse->setCharacterSize(20);
	_nbOfBombDiffuse->setFont("./ressource/AllStarResort.ttf");

	_timeLeft->setCharacterSize(20);
	_timeLeft->setFont("./ressource/AllStarResort.ttf");

	_frameRate->setCharacterSize(20);
	_frameRate->setFont("./ressource/AllStarResort.ttf");


}

Chud::~Chud() {
	// TODO Auto-generated destructor stub
}

void Chud::update(int nbBomb, int nbBombDiffuse, float bombTimer, float bombTimerMax, bool win, float delta)
{
	_nbBomb = nbBomb;
	_nbBombDiffuse = nbBombDiffuse;
	_bombTimer = bombTimer;
	_bombTimerMax = bombTimerMax;

	char buff[256] = { '\0' };
	sprintf(buff,"%s%d",_numberOfBomb.c_str(),_nbBomb);
	_nbOfBomb->setText(std::string(buff));
	_nbOfBomb->setAxisAlignment(osgText::Text::SCREEN);
	_nbOfBomb->setPosition( osg::Vec3(0,float(_height)/1.3f,0) );
	//_nbOfBomb->setColor( osg::Vec4(199, 77, 15, 1) );

	sprintf(buff,"%s%d",_numberOfBombDiffuse.c_str(),_nbBombDiffuse);
	_nbOfBombDiffuse->setText(std::string(buff));
	_nbOfBombDiffuse->setAxisAlignment(osgText::Text::SCREEN);
	_nbOfBombDiffuse->setPosition( osg::Vec3(0,float(_height)/1.35f,0) );
	//_nbOfBomb->setColor( osg::Vec4(199, 77, 15, 1) );

	if(win!=true)
		sprintf(buff,"Time left %.2f",((_bombTimerMax-_bombTimer>0)?_bombTimerMax-_bombTimer:0.0f));
	else
		sprintf(buff,"You Win!");
	_timeLeft->setText(std::string(buff));
	_timeLeft->setAxisAlignment(osgText::Text::SCREEN);
	_timeLeft->setPosition( osg::Vec3(float(_width)/2.f,float(_height)/1.3f,0) );

	sprintf(buff,"FPS: %.1f",(float)1/delta);
	_frameRate->setText(std::string(buff));
	_frameRate->setAxisAlignment(osgText::Text::SCREEN);
	_frameRate->setPosition( osg::Vec3(float(_width)-float(_width)/5.f,float(_height)/1.3f,0) );
}
